package chessengine;

import chesspieces.Piece;
import java.util.*;

public class ChessBoard {

    public char[][] board;
    public Piece[] pieces;
    private char[] values = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
    public int[][] boardState = new int[8][8];
    public int[][] oldBoardState = boardState;

    public ChessBoard() {

        this.pieces = new Piece[32];
        board = new char[8][8];
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                board[x][y] = ' ';
            }
        }
    }

    public ChessBoard(ChessBoard board) {
        this.board = board.board;
    }

    public void Initialize() {
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                if (y == 1) {
                    board[x][y] = 'p';
                }
                if (y == 0) {
                    if (x == 0 || x == 7) {
                        board[x][y] = 'r';
                    }
                    if (x == 1 || x == 6) {
                        board[x][y] = 'h';
                    }
                    if (x == 2 || x == 5) {
                        board[x][y] = 'b';
                    }
                    if (x == 3) {
                        board[x][y] = 'q';
                    }
                    if (x == 4) {
                        board[x][y] = 'k';
                    }
                }

                if (y == 6) {
                    board[x][y] = 'P';
                }
                if (y == 7) {
                    if (x == 0 || x == 7) {
                        board[x][y] = 'R';
                    }
                    if (x == 1 || x == 6) {
                        board[x][y] = 'H';
                    }
                    if (x == 2 || x == 5) {
                        board[x][y] = 'B';
                    }
                    if (x == 3) {
                        board[x][y] = 'Q';
                    }
                    if (x == 4) {
                        board[x][y] = 'K';
                    }
                }

            }
        }

        pieces = new Piece[32];
        for (int i = 0; i < 32; i++) {
            //pieces[i] = new Piece();
        }

    }

    public Piece getPieceAt(int x, int y) {
        for (int i = 0; i < pieces.length; i++) {
            if (pieces[i].posX == x && pieces[i].posY == y) {
                return pieces[i];
            }
        }
        return null;
    }

    public Piece getPiece(char val) {
        for (int i = 0; i < pieces.length; i++) {
            if (pieces[i].val == val) {
                return pieces[i];
            }
        }
        return null;
    }

    public void Initialize(Piece[] pieces) {
        this.pieces = pieces;
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                board[x][y] = ' ';
            }
        }
        for (int i = 0; i < pieces.length; i++) {
            if (pieces[i] != null) {
                board[pieces[i].posX][pieces[i].posY] = pieces[i].getChar();
            }
        }
    }

    public void Update() {
        this.oldBoardState = this.boardState;
        this.boardState = new int[8][8];
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                board[x][y] = ' ';
            }
        }
        for (int i = 0; i < pieces.length; i++) {
            if (pieces[i] != null) {
                board[pieces[i].posX][pieces[i].posY] = pieces[i].getChar();
            }
        }
    }

    public boolean equalsTo(ChessBoard other) {
        int a = 0;
        int b = 0;
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                a += this.boardState[x][y];
                b += other.boardState[x][y];
            }
        }
        return a == b;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        for (int i = 0; i < 8; i++) {
            sb.append('[');
            sb.append(values[i]);
            sb.append(']');
        }
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append("\n\r\n\r");
        for (int y = 7; y >= 0; y--) {

            sb.append('[');
            sb.append(y + 1);
            sb.append(']');
            sb.append(' ');
            sb.append(' ');
            sb.append(' ');

            for (int x = 0; x < 8; x++) {
                sb.append('[');
                sb.append(board[x][y]);
                sb.append(']');
            }

            sb.append(' ');
            sb.append(' ');
            sb.append(' ');
            sb.append('[');
            sb.append(y + 1);
            sb.append(']');
            sb.append("\n\r");
        }
        sb.append("\n\r");
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        for (int i = 0; i < 8; i++) {
            sb.append('[');
            sb.append(values[i]);
            sb.append(']');
        }
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append(' ');
        sb.append("\n\r");
        return sb.toString();
    }
}
